package com.ebo.rongcloud.activitys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import http_utils.interfaces.IHttpPostListener;
import http_utils.utils.Ipost;
import http_utils.utils.URLs;
import io.agora.openvcall.R;
import io.rong.imkit.RongIM;
import utils.IToast;
import utils.Lg;

/**
 * Created by admin on 2017/4/10.
 */

public class FriendFragment extends Fragment {
    public static FriendFragment instance = null;//单例模式

    public static FriendFragment getInstance() {
        if (instance == null) {
            instance = new FriendFragment();
        }

        return instance;
    }

    private View mView;
    private Button mButton_Friend,mButton_Group;

    EditText et_input_to_userid,et_input_to_group;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.friend_fragment, null);
        et_input_to_userid = (EditText) mView.findViewById(R.id.et_input_to_userid);
        et_input_to_group = (EditText) mView.findViewById(R.id.et_input_to_group);
        mButton_Friend = (Button) mView.findViewById(R.id.friend);
        mButton_Friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String to_id = et_input_to_userid.getText().toString();
                if (to_id != null && to_id.length() > 0) {
                    if (RongIM.getInstance() != null) {
                        send_Get_ToUserInfo(to_id);
                    }
                }
            }
        });
        mButton_Group = (Button) mView.findViewById(R.id.into_group);
        mButton_Group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String to_id = et_input_to_group.getText().toString();
                if (to_id != null && to_id.length() > 0) {
                    if (RongIM.getInstance() != null) {
                        RongIM.getInstance().startGroupChat(getActivity(),et_input_to_group.getText().toString(), "群组聊天");
//                        send_Get_ToUserInfo(to_id);
                    }
                }
            }
        });

        return mView;
    }

    String to_userId, to_token, to_nickname, to_rongyun_portrait_uri;

    //登陆，获取token
    void send_Get_ToUserInfo(String uid) {
        Map<String, Object> postParams = new HashMap<String, Object>();
        postParams.put("uid", uid);
        Ipost.send(this.getActivity(), URLs.RONG_GETTOKEN, postParams, new PostGetToken(), true);
    }

    class PostGetToken implements IHttpPostListener {
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(FriendFragment.this.getActivity(), result)) {
                return;
            }
            if (null != result) {
                Lg.d("result=" + result);
                try {
                    JSONObject resultObject = new JSONObject(result);
                    int error = resultObject.getInt("error");
                    //code200(200为正常)",
                    int code = resultObject.getInt("code");
                    switch (code) {
                        //成功
                        case 200:
                            to_userId = resultObject.getString("userId");
                            to_token = resultObject.getString("token");
                            to_nickname = resultObject.getString("nickname");
                            to_rongyun_portrait_uri = resultObject.getString("rongyun_portrait_uri");
//                            mName.setText(to_nickname);
//                           ConversationLoginActivity.insert_UserInfo(to_userId,to_nickname,to_rongyun_portrait_uri);
                            RongIM.getInstance().startPrivateChat(getActivity(), to_userId, to_nickname);
                            break;
                        //其他失败文字提示
                        default:
                            IToast.show(FriendFragment.this.getActivity(), "创建聊天失败，error="+error+",code="+code);
                            break;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    IToast.show(FriendFragment.this.getActivity(), "网络异常：\n" + e.toString());
                }
            }
        }
    }

}
